/*
    TreeViewer - Cross-platform software to draw phylogenetic trees
    Copyright (C) 2023  Giorgio Bianchini, University of Bristol
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

using System.Threading.Tasks;
using TreeViewer;
using System;
using System.Collections.Generic;
using VectSharp;
using System.Runtime.InteropServices;

namespace ColorPickerMenuAction
{
    /// <summary>
    /// This module adds a menu option to open a colour picker dialog window. If this window is opened while a text box is selected,
    /// upon clicking `OK` the contents of the text box are replaced with an hexadecimal representation of the chosen colour. The
    /// shortcut to open this window works even if a text box is focused.
    /// </summary>

    public static class MyModule
    {
        public const string Name = "Color picker";
        public const string HelpText = "Opens a color picker window.";
        public const string Author = "Giorgio Bianchini";
        public static Version Version = new Version("1.0.1");
        public const string Id = "5c99fbfb-a6c6-4e07-915d-670b07d255c8";
        public const ModuleTypes ModuleType = ModuleTypes.MenuAction;

        public static string ItemText { get; } = "Color picker";
        public static string ParentMenu { get; } = "Edit";
        public static string GroupName { get; } = "Color picker";
        public static Avalonia.AvaloniaProperty PropertyAffectingEnabled { get; } = null;

        public static List<(Avalonia.Input.Key, Avalonia.Input.KeyModifiers)> ShortcutKeys { get; } = new List<(Avalonia.Input.Key, Avalonia.Input.KeyModifiers)>() { (Avalonia.Input.Key.C, Avalonia.Input.KeyModifiers.Control | Avalonia.Input.KeyModifiers.Shift) };
        public static bool TriggerInTextBox { get; } = true;

        public static double GroupIndex { get; } = 3;
        public static bool IsLargeButton { get; } = true;
        public static List<(string, Func<double, VectSharp.Page>)> SubItems { get; } = new List<(string, Func<double, VectSharp.Page>)>();

        private static string Icon32Base64 = "iVBORw0KGgoAAAANSUhEUgAAACA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IB7eJhzeJh4d9HF8QDo+Ii2PSKsFuT0md0ssLmnqyPXvqLn5ugk3X1dz866aW9cablEvfpQnd5DT3wxftgOb41GR54B7HzC7NZFr74+1e5evXR13PNWJJy4q4qIiLSrScUNcNuRVQ15NyN0ECw3EJV2daCuhLQ60CCv4p7H4VwRgGB8R+V/gN/fN3nuI/lhw/vfSVvV9aV4Rb9SkVU1aNqRlQz4pAG2DdU0PkZas6syqElaeAeBTm//nFQMYxr5IB3wc+PhD9tx3JqnuS1X9K6mqXp+2alJuyF1N7poeoO0BLDSQl6xq48QpK4QWeWrz87/lPXDV8YB95m7P4p7E8ucyh2/JHLnMEZYX5LyAdIylY8SO2Z7Ba6TmBvraX5Yn/+dVATAe77Q/ruZsvwlOfsg4frux+H5j8XpjASwQjtlFut/nLyay0QOvCoCLxr7t10uWr4Fwl7H8LmG59QH5sz/9/9b17fHt8Wob/wcqEtIqn4oqYwAAAABJRU5ErkJggg==";
        private static string Icon64Base64 = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAAOwAAADsAEnxA+tAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAADzdJREFUeJztm2usbVdVx39jzDnXfpxz7r1VKESiAk1K5IOGxJgQQxNCTcBEJFgwKEEToTxE2tpAlaTmktgSDVga9INoDAmGGD7IN6OJFFJIExRFqoFigKqpRSzyqHc/15xz+GHOtfba++x77j23vaDSmYy7Hnudvdf/N/5jzLXW3he+x4dc6R/azTbF8QmMMcJfovwVgb+T98m3nswTvNrjigDYL9uYEfcBL9zz8tdR/pHMP2E8iOOzJL4kH5BvP7FTvTrj1AAME17PR4GfPfGvt15LQPwWsvoy1v4rkr6CrR+D9SPk9BisvoaPj4POCYsl3/p2YvIva37gq0nOn8+nPUcAe9WrHM98pmf1H2MYTVldmOIWh0h7lhQP5YP3/8Wx07ysN/4VewXGR4+L3Bn9awlowVqQtqzvCzvxtceR9Qrax6FdQ3s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NuF9L4m5MjSM1jnTkyW2p/bwIpGXAVk0pgXURn9sAbcBSjbxi7WDtpYSDlgJBGX1h33lc8f8ZuhrjNvv6s4zHbzXmr0vMrk1cIDMn5Rk5zsnrGbZakNdz8nqBtQusXWJxCXGBpSXCiumRcO5pnnPXBM6q5xzNN39JPvx9+z7zfxWAfpjJLTx4vdK+NjF/TWZ2XWZOZkZmQc5zLC5KtHUZl5CWWFogtuLwrHDuTOCcBM7hP/YG+fCN+z7qu1YCJw4Ruxe+CNwJ3HmbffK5Gf9zEF5uND+RNTTWNOQmYATMyi8izAJWvxuLrFiJsASW6Kcu+lHfMVFP0jhvH/cX+K/nR2Y3wuplxuLHjMXTYYHVoIay4gyOszQvebt86L597/d/DsC+8Rb7g8Mxy+tg9gJh9UJj8WxY/CAsnu1YTQ6wZ52XP3v0km/0/3H8od0cLn3UU+Op8dR4anyPjv8Bb/o/dH7X320AAAAASUVORK5CYII=";

        public static Page GetIcon(double scaling)
        {
            byte[] bytes;

            if (scaling <= 1)
            {

                bytes = Convert.FromBase64String(Icon32Base64);
            }
            else if (scaling <= 1.5)
            {
                bytes = Convert.FromBase64String(Icon48Base64);
            }
            else
            {
                bytes = Convert.FromBase64String(Icon64Base64);
            }

            IntPtr imagePtr = Marshal.AllocHGlobal(bytes.Length);
            Marshal.Copy(bytes, 0, imagePtr, bytes.Length);

            RasterImage icon;

            try
            {
                icon = new VectSharp.MuPDFUtils.RasterImageStream(imagePtr, bytes.Length, MuPDFCore.InputFileTypes.PNG);
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
            finally
            {
                Marshal.FreeHGlobal(imagePtr);
            }

            Page pag = new Page(16, 16);
            pag.Graphics.DrawRasterImage(0, 0, 16, 16, icon);

            return pag;
        }

        public static List<bool> IsEnabled(MainWindow window)
        {
            return new List<bool>() { true };
        }

        public static async Task PerformAction(int index, MainWindow window)
        {
            if (Avalonia.Input.KeyboardDevice.Instance.FocusedElement is Avalonia.Controls.TextBox box && box.IsEffectivelyVisible)
            {
                ColourPickerWindow win = new ColourPickerWindow() { FontFamily = window.FontFamily, FontSize = window.FontSize, Icon = window.Icon };
                Avalonia.Media.Color? col = await win.ShowDialog(window);

                if (col != null)
                {
                    VectSharp.Colour colVectSharp = col.Value.ToVectSharp();
                    box.Text = colVectSharp.ToCSSString(colVectSharp.A != 1);
                }
            }
            else
            {
                ChildWindow win = new ChildWindow() { FontFamily = window.FontFamily, FontSize = window.FontSize, Title = "Color picker", SizeToContent = Avalonia.Controls.SizeToContent.WidthAndHeight, Icon = window.Icon };
                win.Content = new AvaloniaColorPicker.ColorPicker() { FontFamily = window.FontFamily };
                await win.ShowDialog(window);
            }
        }
    }
}